package com.cx.controller;

import com.cx.entity.RestResp;
import com.cx.entity.ZtCustomer;
import com.cx.service.ZtCustomerService;
import com.cx.utils.ExcelUtils;
import com.cx.utils.UtilsFields;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: CustomerController
 * @Description: zt客户管理
 * @Author: cx
 * @Date: 2024/2/27 11:10
 **/

@RestController
@RequestMapping("/customer")
@CrossOrigin
public class CustomerController {
    @Autowired
    private ZtCustomerService ztCustomerService;

    @Autowired
    private ExcelUtils excelUtils;

    @Value("${src.sdk}")
    private String resource;


    @PostMapping("/list")
    public RestResp list(@RequestBody ZtCustomer ztCustomer) {
        List<ZtCustomer> list = ztCustomerService.list(ztCustomer);
        return RestResp.success(list);
    }

    /**
     * @return com.cx.entity.RestResp
     * @Description 新增/更新客户接口
     * @Param No such property: code for class: Script1
     * @Date 2024/3/8 9:58
     * @Author cx
     **/
    @PostMapping("/add")
    public RestResp addCustomer(@RequestBody ZtCustomer ztCustomer) {
        ztCustomer.setCreateTime(new Date());
        ztCustomerService.add(ztCustomer);
        return RestResp.success();
    }

    @DeleteMapping("/delete")
    public RestResp deleteCustomer(@RequestParam List<Integer> ids) {
        int i = ztCustomerService.deleteCustomer(ids);

        return RestResp.success(i);
    }

    //   下载模板
    @GetMapping("/template")
    public ResponseEntity<InputStreamResource> downloadTemplate(@ModelAttribute ZtCustomer params) throws IOException {
        String url = resource + UtilsFields.CUSTOMER_TABLE_EXCEL;
        try {

            if (Files.exists(Paths.get(url))) {
                System.out.println("文件存在。");
            } else {
                System.out.println("文件不存在。");
                excelUtils.exportCustomsTemplate(url);
            }
        } catch (Exception e) {
            System.out.println("新建客户Excel文件失败");

        }

        FileSystemResource fileResource = new FileSystemResource(url);
        InputStreamResource resource = new InputStreamResource(fileResource.getInputStream());

        return ResponseEntity.ok()
                .contentType(MediaType.parseMediaType("application/octet-stream"))
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + fileResource.getFilename())
                .body(resource);


    }


    // 数据客户导出
    @GetMapping("/export")
    public ResponseEntity<InputStreamResource> exportCustomer(@ModelAttribute ZtCustomer ztCustomer) throws Exception {

            List<ZtCustomer> list = ztCustomerService.list(ztCustomer);
            String url = excelUtils.exportCustoms(list);



        FileSystemResource fileResource = new FileSystemResource(url);
        InputStreamResource resource = new InputStreamResource(fileResource.getInputStream());

        return ResponseEntity.ok()
                .contentType(MediaType.parseMediaType("application/octet-stream"))
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + fileResource.getFilename())
                .body(resource);
    }

    public static void main(String[] args) {

    }


}
